package com.neusoft.neuiotms.dcam.fixed.repository.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.model.QWarehouseRecord;
import com.neusoft.neuiotms.dcam.fixed.model.WarehouseRecord;
import com.neusoft.neuiotms.dcam.fixed.repository.CustomizedWarehouseRecordRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedWarehouseRecordRepositoryImpl extends AbstractRepository
        implements CustomizedWarehouseRecordRepository {

    @Override
    public Page<WarehouseRecord> search(final Pageable pageable, final Searchable searchable) {
        final QWarehouseRecord qWarehouseRecord = QWarehouseRecord.warehouseRecord;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<WarehouseRecord> query = this.query().selectFrom(qWarehouseRecord);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    protected Class<?> getModelClass() {
        return WarehouseRecord.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QWarehouseRecord qWarehouseRecord = QWarehouseRecord.warehouseRecord;

        where.or(super.containsStr(qWarehouseRecord.name, searchable, "name"));
        where.or(super.equalsStr(qWarehouseRecord.location, searchable, "location"));

        return where;
    }
}
